#!/usr/bin/perl

use lib::abs qw( base_lib lib );
use JIP::ToolSet; # load my commonly-used modules in a single import

use File::Copy qw( move copy );          # CPAN modules
use File::Spec::Functions qw( catfile ); #
use FindBin qw( $RealBin );              #
use English qw( -no_match_vars );        #
use Carp qw( croak );                    #

use Configurator;                # base lib
use Log;                         #
use Proc::Lock;                  #
use JIP::Utils qw( getLogName ); #

# searcher custom modules
use Filter;
use Filesystem qw(
    getCrawlerOutputListing
    getFilterOutputListing
    parseCrawlerFileName
);

#-- init settings --------------------------------------------------------------
my $conf = Configurator->new( conf => 'robo_conf.pm' )->getSettings;

#-- init log -------------------------------------------------------------------
our $log = Log->new(
    log_file  => catfile( $conf->filter->log_dir, getLogName() ),
    log_level => $conf->filter->log_level,
);

#-- signal handlers ------------------------------------------------------------
require( catfile( $RealBin, 'signal_handlers.pl' ) );

#-- setup lock -----------------------------------------------------------------
$log->Note('setup lock');

my $process = Proc::Lock->new( lock_file => $conf->filter->lock_file );

$process->lock;

#-- getting input files --------------------------------------------------------
$log->Note('getting input files...');

if ( $conf->filter->mode eq 'move' ) {
    foreach my $eachFile ( getCrawlerOutputListing $conf->filter->input_dir ) {
        move( $eachFile->relativePath, $conf->filter->scratch_dir )
            or croak 'cannot move '. $eachFile->relativePath .' to SCRATCH_DIR';
    }
}
elsif ( $conf->filter->mode eq 'fetch' ) {
    foreach my $eachFile ( getCrawlerOutputListing $conf->filter->input_dir ) {
        copy( $eachFile->relativePath, $conf->filter->scratch_dir )
            or croak 'cannot copy '. $eachFile->relativePath .' to SCRATCH_DIR';
    }
}
else {
    croak 'unexpected value in filter mode: '. $conf->filter->mode
}

#-- filtering ------------------------------------------------------------------
$log->Note('fire started filtering...');

foreach my $eachFile ( getCrawlerOutputListing $conf->filter->scratch_dir ) {
    my $host = parseCrawlerFileName( $eachFile->name );

    $log->Note( 'process file: '.    $eachFile->relativePath );
    $log->Note( 'host addr: '.       $host->ID );
    $log->Note( 'host identifier: '. $host->IPAddress );

    Filter->new(
        host_id      => $host->ID,
        host_ip      => $host->IPAddress,
        input_file   => $eachFile->relativePath,
        output_dir   => $conf->filter->scratch_dir,
        enable_cache => $conf->filter->enable_cache,
    )->filterLoop;

    unlink $eachFile->relativePath
        or croak 'cannot unlink '. $eachFile->relativePath .': '. $OS_ERROR;
}

#-- finalyze -------------------------------------------------------------------
$log->Note('-- finalyze -----------------------------------------------------');

$log->Note('moving files from SCRATCH_DIR to OUTPUT_DIR:');

foreach my $eachFile ( getFilterOutputListing $conf->filter->scratch_dir ) {
    $log->Note( $eachFile->name );

    move( $eachFile->relativePath, $conf->filter->output_dir )
        or croak 'cannot move '. $eachFile->name .' to OUTPUT_DIR';
}

$log->Note('unset lock...') and $process->unlock;

$log->Note('exiting');

#-- end ------------------------------------------------------------------------
